#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
    vector<int> dx={0,0,1,-1},dy={1,-1,0,0};
    typedef pair<int,int> pii;
    int minTimeToReach(vector<vector<int>>& moveTime) {
        int n=moveTime.size();
        int m=moveTime[0].size();
        vector<vector<int>> d(n,vector<int>(m,INT_MAX));
        d[0][0]=0;
        queue<pii> q;
        q.emplace(0,0);
        while(!q.empty())
        {
            auto [x,y]=q.front();
            q.pop();
            for(int i=0;i<4;i++)
            {
                int cx=x+dx[i];
                int cy=y+dy[i];
                if(cx>=0&&cx<n&&cy>=0&&cy<m&&d[cx][cy]>max(d[x][y],moveTime[cx][cy])+1)
                {
                    d[cx][cy]=max(d[x][y],moveTime[cx][cy])+1;
                    q.emplace(cx,cy);
                }
            }
        }
        return d[n-1][m-1];
    }
};

int main()
{
    vector<vector<int>> vec={{0,4},{4,4}};
    Solution so;
    so.minTimeToReach(vec);
    return 0;
}